package CareerUp.DrawPoints;

import java.util.ArrayList;
import java.util.List;

/**
 * Input a series of points. If there are 2 points, draw a line. 3 points: draw
 * a triangle, 4 points: draw a square. The trick part is(the interviewer said
 * this), implement redo/undo functions.
 * 
 * @author Xi
 * 
 */
public class DrawPoints {

	public static void main(String[] argv) {
		DrawPoints dp = new DrawPoints();
		dp.addPoint(1);
		dp.addPoint(2);
		dp.getResult();

		dp.addPoint(3);
		dp.getResult();

		dp.undo();
		dp.getResult();

		dp.redo();
		dp.getResult();

		dp.addPoint(4);
		dp.getResult();

		dp.undo();
		dp.getResult();

		dp.undo();
		dp.getResult();
	}

	/**
	 * basic idea is keep track of points in two arrays: current and all
	 */

	List<Integer> currentPoints, allPoints;

	DrawPoints() {
		this.currentPoints = new ArrayList<Integer>();
		this.allPoints = new ArrayList<Integer>();
	}

	private void addPoint(Integer i) {
		this.allPoints.add(i);
		this.currentPoints.add(i);
	}

	private void undo() {
		currentPoints.remove(currentPoints.size() - 1);
	}

	private void redo() {
		currentPoints.add(allPoints.get(currentPoints.size()));
	}

	private void getResult() {
		int size = currentPoints.size();

		switch (size) {
		case (2):
			System.out.println("Draw a line with points: " + currentPoints.toString());
			break;
		case (3):
			System.out.println("Draw a triangle with points: " + currentPoints.toString());
			break;
		case (4):
			System.out.println("Draw a rectangle with points: " + currentPoints.toString());
			break;
		}
	}
}
